package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FTWorkAbortActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    EditText etLot;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.tvEqpID)
    TextView tvEqpID;
    @BindView(R.id.tvMainLotQTY)
    TextView tvMainLotQTY;
    @BindView(R.id.tvMainOrderNum)
    TextView tvMainOrderNum;
    @BindView(R.id.tvMainLotState)
    TextView tvMainLotState;
    @BindView(R.id.tvMainStep)
    TextView tvMainStep;
    @BindView(R.id.etComment)
    EditText etComment;
    @BindView(R.id.etMainLot)
    EditText etMainLot;

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ft_work_abort;
    }

    private List<LotInfoEntity.LotBean> ftlotlist;
    private String RESERVED5 = "";

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("测试合批");
        initEdit(etLot, s -> LotUtils.searchLotInfo(FTWorkAbortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvLotQTY.setText(response.getLOT().getMAINQTY());
                tvOrderNum.setText(response.getLINEID());
                tvLotState.setText(response.getLOT().getSTATE());
                tvEqpID.setText(response.getLOT().getEQUIPMENTID());
                RESERVED5 = response.getLOT().getUDF().getRESERVED5();
                ftlotlist = response.getFTLOTLIST();

                if (ftlotlist != null && ftlotlist.size() > 0) {
                    showPopListView(ftlotlist);
                }
            }

            @Override
            public void onFail(Throwable t) {

            }
        }));

        initEdit(etMainLot, s -> LotUtils.searchLotInfo(FTWorkAbortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvMainLotQTY.setText(response.getLOT().getMAINQTY());
                tvMainOrderNum.setText(response.getLINEID());
                tvMainLotState.setText(response.getLOT().getSTATE());
                tvMainStep.setText(response.getLOT().getSTEPNAME());
            }

            @Override
            public void onFail(Throwable t) {

            }
        }));
    }

    private void showPopListView(List<LotInfoEntity.LotBean> ftlotlist){
        Collections.sort(ftlotlist, (o1, o2) -> o1.getSTATE().compareTo(o2.getSTATE()));//使用Collections的sort方法，并且重写compare方法

        String context = "可以合并的测试主批:" + "\r\n";
        for (LotInfoEntity.LotBean lot : ftlotlist) {
            if (lot.getUDF().getRESERVED5().equals("")) {
                String Text = lot.getLOTID();
                String state = lot.getSTATE();
                String qty=lot.getMAINQTY();
                if ("正在作业".equals(state)) {
                    Text = Text + " --- " + qty +" --- "+ lot.getLOCATION();
                } else if ("等待作业".equals(state)) {
                    Text = Text + " --- "+ qty + " --- 未进站";
                }
                context = context + Text + "\r\n";
            }
        }
        if(context != ""){
            showConfirmDialog(context);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                if (getText(etMainLot).equals("")){
//                    showToast("请输入合并主批批号",false);
//                    return;
//                }

                if (getText(etLot).equals("")){
                    showToast("请输入机台尾数批号",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            String msg = response.body().getResponse().getHeader().getRESULTMESSAGE();
                            showConfirmDialog(FTWorkAbortActivity.this, msg == "" ? "提交成功" : msg);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"LOTFTABORT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","DefaultAbort");
                map.addProperty("ACTIONCOMMENT",getText(etComment));
                map.addProperty("MEGERLOTID",getText(etMainLot));
                map.addProperty("EQUIPMENTID",tvEqpID.getText().toString());
                map.addProperty("RESERVED5",RESERVED5);
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(getText(etLot)));
                map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
                NetUtils.requestPostNet("CPC.LOTFTABORT","LOTFTABORT",this,map,callBack);
                break;
        }
    }
}
